const { con, Result } = require('./common');

let getlist = async (rows) => {
	let arr = new Array();
	arr = await rows.map(async (item) => {
		let spl1 = `select * from commodity where commodity_id = ${item.commodity_id}`
		let [rows2] = await con.execute(spl1)
		arr.push(rows2[0])
		return arr
	})
	console.log(arr)
	return arr
}


let Getlist = async (user_id) => {
	let res = new Result();
	let arr = [];
	let sql = `select * from collection where  user_id = ${user_id}`
	let [rows] = await con.execute(sql);
	for (let i = 0; i < rows.length; i++) {
		let sql1 = `select * from commodity where commodity_id = ${rows[i].commodity_id}`;
		let [rows1] = await con.execute(sql1);
		arr[i] = rows1[0];
	}

	res.msg = '查询成功';
	res.data = arr;
	return res;
}

let Insert_coll = async (user_id, commodity_id) => {
	let res = new Result();
	let sql2 = `select * from commodity where commodity_id = ${commodity_id}`;
	let [rows2] = await con.execute(sql2);
	let sql = `insert collection values('${user_id}','${commodity_id}')`;
	let [rows] = await con.execute(sql);
	let sql1 = `update commodity set commodity_collect = ${rows2[0].commodity_collect + 1} where commodity_id = ${commodity_id}`;
	let [rows1] = await con.execute(sql1);
	res.msg = '收藏成功';
	return res;
}
let Delete_coll = async (user_id, commodity_id) => {
	let res = new Result();
	let sql = `delete from collection where user_id=${user_id} and commodity_id = ${commodity_id}`;
	let [rows] = await con.execute(sql);
	let sql2 = `select * from commodity where commodity_id = ${commodity_id}`;
	let [rows2] = await con.execute(sql2);
	let sql1 = `update commodity set commodity_collect = ${rows2[0].commodity_collect - 1} where commodity_id = ${commodity_id}`;
	let [rows1] = await con.execute(sql1);
	res.msg = '取消收藏成功';
	return res;
}
module.exports = { Getlist, Insert_coll, Delete_coll }
